/**
 *
 */
package com.iknight.mgr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.mgr.api.RoleService;
import com.iknight.mgr.core.annotation.ShiroEffectAnnotation;
import com.iknight.mgr.core.utils.IkgStringUtils;
import com.iknight.mgr.mapper.RoleMapper;
import com.iknight.mgr.model.RoleModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jeryzym
 */
@Service
public class RoleServiceImpl implements RoleService<RoleModel, String> {

    @Autowired
    private RoleMapper<RoleModel, String> roleMapper;

    public void save(RoleModel record) {
        // TODO Auto-generated method stub
        if (roleMapper.selectOne(record.getRole_id().toString()) != null) {
            roleMapper.update(record);
        } else {
            roleMapper.insert(record);
        }
    }

    /**
     * 关联删除，角色删除时，应释放角色与菜单、权限、人员关系
     */
    @Transactional
    public void delete(String pkey) {
        // TODO Auto-generated method stub
        roleMapper.releaseRoleFromMenu(pkey);
        roleMapper.releaseRoleFromPerm(pkey);
        roleMapper.releaseRoleFromUser(pkey);
        roleMapper.delete(pkey);
    }

    public RoleModel selectOne(String pkey) {
        // TODO Auto-generated method stub
        return roleMapper.selectOne(pkey);
    }

    public PageInfo<RoleModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        // TODO Auto-generated method stub
        PageHelper.startPage(pageStart, pageSize, true);
        List<RoleModel> result = roleMapper.selectByCond(params);
        PageInfo<RoleModel> page = new PageInfo<RoleModel>(result);
        return page;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Transactional
    public void grantRoleToUser(Map<String, Object> params) {
        // TODO Auto-generated method stub
        String roleId = params.get("role_id").toString();
        roleMapper.releaseRoleFromUser(roleId);

        List ruList = new ArrayList<>();
        List userIds = IkgStringUtils.removeDuplicate((List) params.get("user_id"));

        for (Object userId : userIds) {
            Map map = new HashMap<>();
            map.put("role_id", roleId);
            map.put("user_id", userId.toString());
            ruList.add(map);
        }
        roleMapper.grantRoleToUser(ruList);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Transactional
    @ShiroEffectAnnotation(enable = true)
    public void grantPermToRole(Map<String, Object> params) {
        // TODO Auto-generated method stub
        String roleId = params.get("role_id").toString();
        roleMapper.releaseRoleFromPerm(roleId);

        List prList = new ArrayList<>();
        List permIds = IkgStringUtils.removeDuplicate((List) params.get("perm_id"));

        for (Object permId : permIds) {
            Map map = new HashMap<>();
            map.put("role_id", roleId);
            map.put("perm_id", permId.toString());
            prList.add(map);
        }
        roleMapper.grantPermToRole(prList);
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    @Transactional
    public void grantMenuToRole(Map<String, Object> params) {
        // TODO Auto-generated method stub
        String roleId = params.get("role_id").toString();
        roleMapper.releaseRoleFromMenu(roleId);

        List mrList = new ArrayList<>();
        List menuIds = IkgStringUtils.removeDuplicate((List) params.get("menu_id"));

        for (Object permId : menuIds) {
            Map map = new HashMap<>();
            map.put("role_id", roleId);
            map.put("menu_id", permId.toString());
            mrList.add(map);
        }
        roleMapper.grantMenuToRole(mrList);
    }

    public List<Map> getMenusByRoleId(String roleId) {
        // TODO Auto-generated method stub
        return roleMapper.getMenusByRoleId(roleId);
    }

    public List<Map> getMenusByUserId(String userId) {
        // TODO Auto-generated method stub
        return roleMapper.getMenusByUserId(userId);
    }

    public List<Map> getPermsByUserId(String userId) {
        // TODO Auto-generated method stub
        return roleMapper.getPermsByUserId(userId);
    }

    public List<Map> getMenusByUserIdWithRoleId(String roleId, String userID) {
        // TODO Auto-generated method stub
        return roleMapper.getMenusByUserIdWithRoleId(roleId, userID);
    }

    public List<Map> getPermsByUserIdWithRoleId(String roleId, String userId) {
        // TODO Auto-generated method stub
        return roleMapper.getPermsByUserIdWithRoleId(roleId, userId);
    }

    public List<Map> getRolesByUserId(String userId) {
        // TODO Auto-generated method stub
        return roleMapper.getRolesByUserId(userId);
    }

    @Override
    public Object getRoleDefinedURIByUsername(String username) {
        // TODO Auto-generated method stub
        return roleMapper.getRoleDefinedURIByUsername(username);
    }

    @Override
    public List<Map> getMenusByUsername(String username) {
        // TODO Auto-generated method stub
        return roleMapper.getMenusByUsername(username);
    }

    @Override
    public List<Map> getPermsByUsername(String username) {
        // TODO Auto-generated method stub
        return roleMapper.getPermsByUsername(username);
    }

    @Override
    public List<Map> getRolesByUsername(String username) {
        // TODO Auto-generated method stub
        return roleMapper.getRolesByUsername(username);
    }

    @Override
    public RoleModel getRecordByCode(String roleCode) {
        // TODO Auto-generated method stub
        return roleMapper.getRecordByCode(roleCode);
    }

    @Override
    public PageInfo<Map> getUsersByRoleId(String roleId, int pageStart, int pageSize) {
        // TODO Auto-generated method stub
        // return roleMapper.getUsersByRoleId(roleId);
        if (pageStart > 0 && pageSize > 0) {
            PageHelper.startPage(pageStart, pageSize, true);
        }
        List<Map> result = roleMapper.getUsersByRoleId(roleId);
        PageInfo<Map> page = new PageInfo<Map>(result);
        return page;
    }

//    public PageInfo<Map> getPermsByRoleId(String roleId, int pageStart, int pageSize) {
//        // TODO Auto-generated method stub
//        if (pageStart > 0 && pageSize > 0) {
//            PageHelper.startPage(pageStart, pageSize, true);
//        }
//        List<Map> result = roleMapper.getPermsByRoleId(roleId);
//        PageInfo<Map> page = new PageInfo<>(result);
//        return page;
//    }

    public List<Map> getUsersByRoleId(String roleId) {
        return roleMapper.getUsersByRoleId(roleId);
    }

    public List<Map> getPermsByRoleId(String roleId) {
        return roleMapper.getPermsByRoleId(roleId);
    }
}
